#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import rospy
import json
from flask import Blueprint,request, Response
import os

from parser.ota_parser import OTA_Parser
from tool.common import *
from tool import Log_Parser
log = Log_Parser()

ota_parser = OTA_Parser()
ota_server = Blueprint("/ota",__name__)

#---------------------------------#
#******#   flask-网络端口验证   #******#
#---------------------------------#
@ota_server.before_request
def before():
    res_data = before_user(request)
    if res_data:
        return Response(json.dumps(res_data),  mimetype='application/json')

# 机器是否重启
@ota_server.route('/robot_reboot',methods=['POST'])
def Post_RobotReboot():
    data = request.json
    log.LogInfo(rospy.get_name(), "request_info: {}, data: {}".format(getRequestInfo(request), "请求重启操作。"))
    res_data = ota_parser.RobotReboot(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取本地版本信息  flag=0 获取版本信息 1获取版本安装进度
# 旧接口兼容
@ota_server.route('/check_update',methods=['GET','POST'])
def Post_GetVersionInfo():
    res_data = ota_parser.GetVersionInfo()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取本地版本信息
# 新接口，后续统一对外使用
@ota_server.route('/get_version_info',methods=['GET','POST'])
def Post_GetVersionInfoV3():
    res_data = ota_parser.GetVersionInfoV3()
    return Response(json.dumps(res_data),  mimetype='application/json')


# 检查版本更新
@ota_server.route('/check_version_update',methods=['GET','POST'])
def Post_CheckVersionUpdate():
    res_data = ota_parser.CheckVersionUpdate()
    return Response(json.dumps(res_data),  mimetype='application/json')


# 检查版本更新
@ota_server.route('/start_updating',methods=['POST'])
def Post_StartUpdating():
    data = request.json
    res_data = ota_parser.StartUpdating(data)
    return Response(json.dumps(res_data),  mimetype='application/json')






